﻿import sys
import Hobot.GPIO as GPIO
import time
import random

# 初始化全局变量
tri_gpio = 11

outer_control_angle = 0;

def setup():
   
    # 从命令行参数中读取并转换为整数
    global outer_control_angle;
    if(sys.argv[1]):

        outer_control_angle = int(sys.argv[1])
    


    # GPIO.cleanup()  # 清理之前的配置
    GPIO.setmode(GPIO.BOARD)  # 设置GPIO模式
  
    GPIO.setup(tri_gpio, GPIO.OUT, initial=GPIO.LOW)





# state = True

loopCount = 0;

def loop():
    global loopCount
    while True:

        # state = not state
        # angle = 90 if state else 0

        angle = outer_control_angle

        # 0.5 → 0
        # 1 → 45
        # 1.5 → 90
        # 2 → 135
        # 2.5 → 180

        angle_time = 0.5+2*(angle/180)
        
        print("转向角度{0}".format(angle))
        # 设置高电平
        GPIO.output(tri_gpio, GPIO.HIGH)
        # 设置休眠  毫秒
        time.sleep(angle_time/1000)
        # 设置低电平
        GPIO.output(tri_gpio, GPIO.LOW)
        # 等待1秒
        time.sleep(0.05)
        loopCount+=1
        if loopCount>3:
            break
       

def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    setup()
    try:
        loop()
    except KeyboardInterrupt:
        destroy()
    finally:
        destroy()
